package com.tencent.hummingbird.portal.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tencent.hummingbird.portal.exception.ServiceException;
import com.tencent.hummingbird.portal.mapper.QuestionMapper;
import com.tencent.hummingbird.portal.mapper.UserMapper;
import com.tencent.hummingbird.portal.pojo.Question;
import com.tencent.hummingbird.portal.pojo.User;
import com.tencent.hummingbird.portal.r.R;
import com.tencent.hummingbird.portal.service.IQuestionService;
import com.tencent.hummingbird.portal.service.IUserService;
import com.tencent.hummingbird.portal.vo.AskVO;
import com.tencent.hummingbird.portal.vo.EditQuestionVO;
import com.tencent.hummingbird.portal.vo.MyQuestionVO;
import com.tencent.hummingbird.portal.vo.QuestionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxb
 * @since 2024-01-10
 */
@Tag(name = "问题",description = "问题控制器")
@RestController
@RequestMapping("/portal/question")
@Slf4j
public class QuestionController {
    @Autowired
    private IQuestionService questionService;
    //把pageSize当成参数
    @GetMapping("/my") //自动从secretary中获取到UserDetails对象
    public R<List<Question>> my(@AuthenticationPrincipal UserDetails user,Integer pageNum,Integer pageSize){
        PageInfo<Question> pageInfo = questionService.getMyQuestions(user.getUsername(),pageNum,pageSize);
        return R.ok(pageInfo);
    }
    @PostMapping("/create")
    public R creatQuestion(@AuthenticationPrincipal UserDetails userDetails,
                           @RequestBody @Validated QuestionVO questionVO,
                           BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String message = bindingResult.getFieldError().getDefaultMessage();
            return R.unproecsableEntity(message);
        }

        questionService.saveQuestion(questionVO, userDetails.getUsername());
        return R.created("发布问题成功");


    }
    @GetMapping("/teacher")
    public R<PageInfo<Question>> teacher(@AuthenticationPrincipal UserDetails teacher,Integer pageNum,Integer pageSize){
        PageInfo<Question> pageInfo = questionService.getQuestionsByTeacherName(teacher.getUsername(),pageNum,pageSize);
        return R.ok(pageInfo);
    }
    @GetMapping("/detail/{id}")
    public R<Question> question(@PathVariable Integer id){
        Question question = questionService.getQuestionById(id);
        return R.ok(question);
    }
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/collect")
    public R<PageInfo<MyQuestionVO>> collect(@AuthenticationPrincipal UserDetails user,Integer pageNum,Integer pageSize){
        User user1 = userMapper.findUserByUsername(user.getUsername());
        PageHelper.startPage(pageNum,pageSize);
        List<MyQuestionVO> questions = questionMapper.findCollectQuestion(user1.getId());
        log.info("pageSize:{}",pageSize);
        PageInfo<MyQuestionVO> pageInfo = new PageInfo<>(questions);
        return R.ok(pageInfo);
    }
    @GetMapping("/ask/{pageNum}/{pageSize}")
    public R<PageInfo<AskVO>> ask(@AuthenticationPrincipal UserDetails userDetails,@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        PageInfo pageInfo =  questionService.getMyAskByUserName(userDetails.getUsername(),pageNum,pageSize);
        return R.ok(pageInfo);
    }

    @GetMapping("/view/{questionId}")
    public R view(@PathVariable Integer questionId){
        log.info("questionId:{}",questionId);
        Integer result = questionService.updateViewsById(questionId);
        return R.ok(result);
    }

    @GetMapping("/editQuestion/{questionId}")
    public R<EditQuestionVO> editQuestion(@PathVariable Integer questionId){
       EditQuestionVO question =  questionService.getEditQuestionVOById(questionId);
       return R.ok(question);
    }

    @PostMapping("/updateQuestion")
    public R updateQuestion(@RequestBody @Validated EditQuestionVO editQuestionVO, BindingResult result){
        if(result.hasErrors()){
            return R.unproecsableEntity(result.getFieldError().getDefaultMessage());
        }
        log.info("editQuestionVO:{}",editQuestionVO);
        questionService.updateQuestion(editQuestionVO);
        return R.ok("修改完成");
    }

    @Operation(summary = "热点问题",description = "显示10条浏览量最高的问题")
    @GetMapping("/host/{pageNum}/{pageSize}")
    public R<PageInfo<Question>> hostQuestions(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        PageInfo<Question> pageInfo =  questionService.selectHostQuestions(pageNum,pageSize);
        log.info("pageInfo:{}",pageInfo);
        return R.ok(pageInfo);

    }

    @GetMapping("/works/{pageNum}/{pageSize}/{status}")
    public R<PageInfo<Question>> works(@AuthenticationPrincipal UserDetails userDetails,
                                       @PathVariable Integer pageNum,
                                       @PathVariable Integer pageSize,
                                       @PathVariable Integer status){
        System.out.println("11111111111111111111111111111111111111111111111111111");
        PageInfo<Question> pageInfo = questionService.selectWork(userDetails.getUsername(),status,pageNum,pageSize);
        log.info("questions:{}",pageInfo);
        return R.ok(pageInfo);

    }
}
